package library.gui.component;

import java.util.List;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Separator;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import library.core.Library.BorrowInfo;
import library.core.book.Book;
import library.core.user.User;
import library.gui.SystemApplication;

public class BookList extends ScrollPane{
	
	private SystemApplication handler;

	private VBox books;
	private ToggleGroup group;
	
	public Book getSelectedBook() {
		var x = group.getSelectedToggle();
		if(x == null) return null;
		return ((BookEntry)x.getUserData()).book;
	}
	
	public void refresh(List<Book> list) {
		books = new VBox();
		books.setPrefHeight(600);
		list.forEach((x)->{
			BookEntry y = new BookEntry(x);
			y.prefWidthProperty().bind(this.widthProperty().add(-40));
			books.getChildren().addAll(y, new Separator());
		});
		this.setContent(books);
	}
	
	public BookList(SystemApplication handler) {
		
		this.handler = handler;
		
		this.group = new ToggleGroup();
//		this.group.selectedToggleProperty().addListener((o, old, nv)->{
//			var x = (BookEntry)o.getValue();
//			var y = ((BookEntry)x.getUserData()).button;
//			if(nv != null)
//				x.setGraphic(BookEntry.wrapView(BookEntry.SELECTED));
//			else
//				x.setGraphic(BookEntry.wrapView(BookEntry.DEFAULT));
//		});
//		this.group.selectedToggleProperty().addListener(new InvalidationListener() {		
//			@Override
//			public void invalidated(Observable observable) {
//				if(observable instanceof ToggleButton toggle) {
//					System.out.println(1);
//				}
//			}
//		});
		books = new VBox();
		books.setPrefHeight(600);
		this.setContent(books);
				
	}
	
	class BookEntry extends BorderPane{
		
		private static final Image DEFAULT = new Image("/b.png"); 
//		private static final Image SELECTED = new Image("/b2.png"); 
		
		private static ImageView wrapView(Image image) {
			ImageView imageView = new ImageView(image);
			imageView.setFitWidth(100);
			imageView.setFitHeight(100);
			return imageView;
		}
		
		private ToggleButton button;
		private VBox desc;
		private VBox info;
		private Book book;
		
		private static Label createInfoEntry(String value) {
			Label label = new Label(value);
			label.setTextFill(Color.SILVER);
			return label;
		}
		
		public BookEntry(Book book) {
			
//			this.setStyle("-fx-border-style: solid; -fx-border-color: silver");
			this.book = book;

			this.button = new ToggleButton("", wrapView(DEFAULT));
			this.button.setToggleGroup(group);
			this.button.setUserData(this);
//			this.button.getStyleClass().set(0, "button");
//			this.setStyle(".button:pressed {\r\n"
//					+ "\r\n"
//					+ "-fx-base-color: black;\r\n"
//					+ "\r\n"
//					+ "}");
			
			Label name = new Label(book.getName()),
				author = new Label("作者：" + book.getAuthor()),
				type = new Label(book.getType()),
				price = new Label("价格：" + book.getPrice());
			name.setFont(new Font("BOLD", 18));
			this.desc = new VBox();
			this.desc.setPadding(new Insets(5));
			this.desc.getChildren().addAll(name, author, type, price);
			
			this.info = new VBox();
			this.info.setAlignment(Pos.CENTER);
			this.info.setPadding(new Insets(10));
			this.info.getChildren().add(createInfoEntry("库存：" + book.getNum()));
			
			User user = handler.getCurrentUser();
			BorrowInfo borrowInfo = handler.getLibrary().borrowed(user, book);
			if(user.isAdmin())
				this.info.getChildren().add(createInfoEntry("在借：" + handler.getLibrary().getBorrowedNum(book)));
			if(!borrowInfo.no()) {
				this.info.getChildren().add(createInfoEntry("借阅时间：" + borrowInfo.getBegin()));
				this.info.getChildren().add(createInfoEntry("归还截止：" + borrowInfo.getEnd()));
			}
			
			this.setLeft(this.button);
			this.setCenter(this.desc);
			this.setRight(this.info);
			
		}
		
	}

}
